package com.rzhkj.box.ctrl;

import com.beust.jcommander.internal.Maps;
import com.rzhkj.box.entity.Account;
import com.rzhkj.box.entity.Box;
import com.rzhkj.box.service.AccountSV;
import com.rzhkj.box.service.BoxSV;
import com.rzhkj.core.entity.BeanRet;
import com.rzhkj.core.exceptions.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 收纳盒控制器
 *
 * @author lixin
 */
@Controller
@RequestMapping("/box")
@Api(value = "收纳盒控制器", description = "收纳盒控制器")
public class BoxCtrl {

    @Resource
    private BoxSV boxSV;

    @Resource
    private AccountSV accountSV;


    /**
     * 添加收纳盒
     *
     * @return BeanRet
     */
    @ApiOperation(value = "添加收纳盒", notes = "添加收纳盒")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "namespace", value = "命名空间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "isCache", value = "是否缓存", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(Box box, String token) {
        Assert.hasText(box.getName(), BaseException.BaseExceptionEnum.Empty_Param.toString());
        Assert.hasText(box.getNamespace(), BaseException.BaseExceptionEnum.Empty_Param.toString());

//        String accountCode = JwtToken.getTokenValue(token, Constants.AccountCode.val.toString());
        Map<String, Object> map = Maps.newHashMap();
//        map.put("code", accountCode);
        Account accountLoad = accountSV.load(map);
        Assert.notNull(accountLoad, BaseException.BaseExceptionEnum.Result_Not_Exist.toString());
//        box.setAccountCode(accountCode);
        boxSV.save(box);
        return BeanRet.create(true, "", box);
    }
}
